import baseUrl from "./baseUrl"
import qs from "./qs"

var request = function(config) {
    return new Promise((resolve, rejected) => {
        var setting = {
            url: baseUrl + config.url,
            method: config.method ? (config.method).toUpperCase() : "GET",
            data: config.data,
            //一定要配置好这个，不然后面登录后，就算拿到了token也无法使用。
            header: {
                Authorization: uni.getStorageSync("token") ? uni.getStorageSync("token") : ""
            },
            timeout: 50000,
            success(res) {
                console.log('request', res, uni.getStorageSync("token"))
                if (res.data.success == true) {
                    resolve(res.data);
                } else {
                    if (res.data.status == 1) {
                        // console.log(res)
                        uni.showModal({
                            title: "登录提示",
                            content: res.data.message,
                            success(result) {
                                if (result.confirm) {
                                    uni.navigateTo({
                                        url: "/pages/user/login/login"
                                    })
                                } else {
                                    uni.showToast({
                                        title: "返回上一页"
                                    })
                                    setTimeout(() => {
                                        uni.navigateBack()
                                        rejected(res.data);
                                    }, 1000)
                                }
                            }
                        })

                    } else {
                        uni.showToast({
                            title: res.data.message,
                            icon: "none"
                        })
                        rejected(res.data);

                    }
                }
            },
            fail(res) {
                rejected(res)
            }
        }
        uni.request(setting)
    })

};
export default request
